<?php

namespace app\service;

use app\model\Member;
use think\facade\Cache;
use think\facade\Request;

class LoginService
{
    // 登录失败次数限制
    const MAX_ATTEMPTS = 5;

    // 锁定时间（秒）
    const LOCKOUT_TIME = 600; // 10分钟

    // 登录失败次数缓存前缀
    private $loginAttemptsPrefix;

    private $loginLockoutPrefix;

    public function __construct($loginAttemptsPrefix='login_attempts:',$loginLockoutPrefix='login_lockout:')
    {
        $this->loginAttemptsPrefix = $loginAttemptsPrefix;
        $this->loginLockoutPrefix = $loginLockoutPrefix;
    }

    /**
     * 检查登录尝试次数
     *
     * @param string $username 用户名
     * @return bool 是否允许登录
     */
    public function checkLoginAttempts($username)
    {
        $ip = Request::ip();
        $cacheKey = $this->loginAttemptsPrefix . md5($username . $ip);

        // 获取当前尝试次数
        $attempts = Cache::store('file')->get($cacheKey, 0);

        // 如果尝试次数超过限制
        if ($attempts >= self::MAX_ATTEMPTS) {
            return false;
        }

        return true;
    }

    /**
     * 记录登录失败
     *
     * @param string $username 用户名
     * @return void
     */
    public function incrementLoginAttempts($username)
    {
        $ip = Request::ip();
        $cacheKey = $this->loginAttemptsPrefix . md5($username . $ip);

        // 获取当前尝试次数
        $attempts = Cache::get($cacheKey, 0);
        $attempts++;

        // 设置缓存，10分钟过期
        Cache::store('file')->set($cacheKey, $attempts, self::LOCKOUT_TIME);

        // 如果达到最大尝试次数，记录锁定时间
        if ($attempts >= self::MAX_ATTEMPTS) {
            $lockoutKey = $this->loginAttemptsPrefix . md5($username . $ip);
            Cache::store('file')->set($lockoutKey, time() + self::LOCKOUT_TIME, self::LOCKOUT_TIME);
        }
    }

    /**
     * 清除登录失败记录
     *
     * @param string $username 用户名
     * @return void
     */
    public function clearLoginAttempts($username)
    {
        $ip = Request::ip();
        $cacheKey = $this->loginAttemptsPrefix . md5($username . $ip);
        $lockoutKey = $this->loginLockoutPrefix . md5($username . $ip);

        // 清除缓存
        Cache::store('file')->delete($cacheKey);
        // 清除锁定时间
        Cache::store('file')->delete($lockoutKey);
    }

    /**
     * 获取剩余可尝试次数
     *
     * @param string $username 用户名
     * @return int 剩余次数
     */
    public function getRemainingAttempts($username)
    {
        $ip = Request::ip();
        $cacheKey = $this->loginAttemptsPrefix . md5($username . $ip);

        // 获取当前尝试次数
        $attempts = Cache::store('file')->get($cacheKey, 0);

        return max(0, self::MAX_ATTEMPTS - $attempts);
    }

    /**
     * 获取锁定剩余时间（秒）
     *
     * @param string $username 用户名
     * @return int 剩余锁定时间（秒）
     */
    public function getLockoutTime($username)
    {
        $ip = Request::ip();
        $lockoutKey = $this->loginLockoutPrefix . md5($username . $ip);

        $lockoutEndTime = Cache::store('file')->get($lockoutKey, 0);

        if ($lockoutEndTime > 0) {
            $remainingTime = $lockoutEndTime - time();
            return $remainingTime > 0 ? $remainingTime : 0;
        }
        return 0;
    }
}
